﻿@page "/mycontract"
@rendermode InteractiveServer
@using BlazorApp派单.auth
@using System.Diagnostics.CodeAnalysis
@inject ToastService ToastService
@inject AuthenticationStateProvider customAuthenticationStateProvider
@inject SqlSugarScope _SqlSugarScope
@inject NavigationManager NavigationManager

<BootstrapBlazorRoot>

    <h3>负责项目</h3>

<DemoBlock Title="@username"
           Name="IsStriped">
        <div class="row g-3">
            <div class="col-12 col-sm-6">
                <BootstrapInput PlaceHolder="项目名称" @bind-Value="@bus.项目名称"
                                DisplayText="项目名称" ShowLabel="true" />
            </div>
        </div>
        <div class="row g-3">
            <div class="col-12 col-sm-6">
                <BootstrapInput PlaceHolder="合同编号" @bind-Value="@bus.合同编号"
                                ShowLabel="true"
                                DisplayText="合同编号" />
            </div>
            <Button Icon="fa-solid fa-arrows-rotate" Text="刷新" OnClick="OnClick" class="col-12 col-sm-auto" />
        </div>
        <div class="row g-3">
            <div class="col-12 col-sm-6">
               
            </div>
           
        </div>


        <Table TItem="Bus_合同主表" Items="@Items" @ref="TableRows" IsStriped="true" IsBordered="true"
               ShowToolbar="false" ShowDefaultButtons="false"
               ShowExtendButtons="true" ShowExtendEditButton="false" ShowExtendDeleteButton="false" ExtendButtonColumnWidth="240">
        <TableColumns>
                <TableColumn @bind-Field="@context.合同状态" />
                <TableColumn @bind-Field="@context.合同类型" />
                <TableColumn @bind-Field="@context.合同编号" />
                <TableColumn @bind-Field="@context.档案编号" />
                <TableColumn @bind-Field="@context.乙方项目经理" />
                <TableColumn @bind-Field="@context.项目名称" />
                <TableColumn @bind-Field="@context.合同甲方名称" />
                <TableColumn @bind-Field="@context.工期开始" />
                <TableColumn @bind-Field="@context.工期结束" />
                <TableColumn @bind-Field="@context.合同摘要" Width="120" TextEllipsis="true" />
        </TableColumns>
            <RowButtonTemplate>
                <TableCellButton Icon="fa-solid fa-pen" Text="查看任务" OnClick="@(() => OnRowButtonClick(context, "查看任务"))" />
            </RowButtonTemplate>
    </Table>


       

</DemoBlock>
</BootstrapBlazorRoot>

@code {

    private string username;

    protected override async Task OnInitializedAsync()
    {
        var authState = await customAuthenticationStateProvider.GetAuthenticationStateAsync();
        var user = authState.User;
        username = user.Identity.Name;
    }

    [NotNull]
    private List<Bus_合同主表>? Items { get; set; }
    private Bus_合同主表 bus = new Bus_合同主表();


    private void OnClick()
    {
        查询();
    }

    private void 查询()
    {
        //可以查看的人员
        Dic_人员数据权限 dic_人员数据权限 = _SqlSugarScope.Queryable<Dic_人员数据权限>()
                                                    .Where(t => t.人员编号 == username).First();
        List<string> 乙方项目经理s = new List<string>();
        if (dic_人员数据权限 != null && string.IsNullOrEmpty(dic_人员数据权限.人员数据) == false)
        {
            乙方项目经理s.AddRange(dic_人员数据权限?.人员数据.Split(',').ToList());
        }

        //以 ',' 拼接乙方项目经理s
        乙方项目经理s.Add(username);

        Items = _SqlSugarScope.Queryable<Bus_合同主表>()
                .WhereIF(!string.IsNullOrEmpty(bus.合同编号), t => t.合同编号.Contains(bus.合同编号))
                .WhereIF(!string.IsNullOrEmpty(bus.合同类型), t => t.合同类型.Contains(bus.合同类型))
                .WhereIF(!string.IsNullOrEmpty(bus.项目名称), t => t.项目名称.Contains(bus.项目名称))
                .WhereIF(!string.IsNullOrEmpty(bus.合同甲方名称), t => t.合同甲方名称.Contains(bus.合同甲方名称))
                .WhereIF(!string.IsNullOrEmpty(bus.中标通知书份数), t => t.中标通知书份数 == bus.中标通知书份数)
                .WhereIF(!string.IsNullOrEmpty(bus.合同份数), t => t.合同份数 == bus.合同份数)
                .WhereIF(!string.IsNullOrEmpty(bus.竣工验收证明份数), t => t.竣工验收证明份数 == bus.竣工验收证明份数)
                .WhereIF(!string.IsNullOrEmpty(bus.审计报告份数), t => t.审计报告份数 == bus.审计报告份数)
                .WhereIF(bus.工期开始 != null, t => t.工期开始 == bus.工期开始)
                .WhereIF(bus.工期结束 != null, t => t.工期结束 == bus.工期结束)
                .WhereIF(bus.历经天数 != null, t => t.历经天数 == bus.历经天数)
                .WhereIF(bus.合同到期 != null, t => t.合同到期 == bus.合同到期)
                .WhereIF(bus.合同金额 != null, t => t.合同金额 == bus.合同金额)
                .WhereIF(bus.审定金额 != null, t => t.审定金额 == bus.审定金额)
                .WhereIF(bus.履约保证金 != null, t => t.履约保证金 == bus.履约保证金)
                .WhereIF(bus.开票含税收入 != null, t => t.开票含税收入 == bus.开票含税收入)
                .WhereIF(bus.已收款 != null, t => t.已收款 == bus.已收款)
                .WhereIF(bus.未收款 != null, t => t.未收款 == bus.未收款)
                .WhereIF(bus.未开票含税收入 != null, t => t.未开票含税收入 == bus.未开票含税收入)
                .WhereIF(bus.开票进度 != null, t => t.开票进度 == bus.开票进度)
                .WhereIF(bus.收款进度 != null, t => t.收款进度 == bus.收款进度)
                //.WhereIF(!string.IsNullOrEmpty(bus.乙方项目经理), t => t.乙方项目经理.Contains(bus.乙方项目经理))
                .Where(t => 乙方项目经理s.Contains(t.乙方项目经理))
                .WhereIF(!string.IsNullOrEmpty(bus.合同摘要), t => t.合同摘要.Contains(bus.合同摘要))
                .OrderBy(t => t.合同编号, SqlSugar.OrderByType.Desc)
                .ToList();
    }

    [NotNull]
    private Table<Bus_合同主表>? TableRows { get; set; }
    //写OnRowButtonClick方法


    private async Task OnRowButtonClick(Bus_合同主表 item, string text)
    {
        Guid guid = item.guid;
        //跳转到派单页面，并携带guid
        NavigationManager.NavigateTo($"/任务列表/{guid}");
    }
}
